package com.ruoyi.blog.controller;

import com.ruoyi.blog.domain.BlogTag;
import com.ruoyi.blog.service.IBlogTagService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.excel.ExcelUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 博客标签Controller
 *
 * @author 刘哲贤
 * @date 2022-08-15
 */
@RestController
@RequestMapping("/admin/tag")
public class BlogTagController extends BaseController {
    @Resource
    private IBlogTagService blogTagService;

    /**
     * 查询博客标签列表
     */
    @PreAuthorize("@ss.hasPermi('admin:tag:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogTag blogTag) {
        startPage();
        List<BlogTag> list = blogTagService.selectBlogTagList(blogTag);
        return getDataTable(list);
    }

    /**
     * 导出博客标签列表
     */
    @PreAuthorize("@ss.hasPermi('admin:tag:export')")
    @Log(title = "博客标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogTag blogTag) {
        List<BlogTag> list = blogTagService.selectBlogTagList(blogTag);
        ExcelUtils<BlogTag> util = new ExcelUtils<BlogTag>(BlogTag.class);
        util.exportData(response, list, "博客标签数据");
    }

    /**
     * 获取博客标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:tag:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(blogTagService.selectBlogTagById(id));
    }

    /**
     * 新增博客标签
     */
    @PreAuthorize("@ss.hasPermi('admin:tag:add')")
    @Log(title = "博客标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogTag blogTag) {
        return toAjax(blogTagService.insertBlogTag(blogTag));
    }

    /**
     * 修改博客标签
     */
    @PreAuthorize("@ss.hasPermi('admin:tag:edit')")
    @Log(title = "博客标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogTag blogTag) {
        return toAjax(blogTagService.updateBlogTag(blogTag));
    }

    /**
     * 删除博客标签
     */
    @PreAuthorize("@ss.hasPermi('admin:tag:remove')")
    @Log(title = "博客标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(blogTagService.deleteBlogTagByIds(ids));
    }


    /**
     * 查询博客标签列表不分页
     */
    @GetMapping("/tagList")
    public TableDataInfo tagList(BlogTag blogTag) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        blogTag.setDeleted(0);
        List<BlogTag> list = blogTagService.selectBlogTagList(blogTag);
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }

    /**
     * 新增博客标签
     */
//    @PreAuthorize("@ss.hasPermi('admin:tag:add')")
//    @Log(title = "博客标签", businessType = BusinessType.INSERT)
//    @PostMapping("/addTagList")
//    public TableDataInfo addTagList(@RequestBody List<BlogTag> list) {
//        TableDataInfo tableDataInfo = new TableDataInfo();
//        tableDataInfo.setRows(blogTagService.saveBlogTagList(list));
//        return tableDataInfo;
//    }
}
